package com.yydm.serviceedu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yydm.servicebase.exceptionhandler.LException;
import com.yydm.serviceedu.entity.AutomobileCategory;
import com.yydm.serviceedu.entity.CategoryData;
import com.yydm.serviceedu.service.AutomobileCategoryService;

/**
 * @Classname CategoryExcelListener
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/6/25 18:52
 * @Created by liu25
 */
public class CategoryExcelListener extends AnalysisEventListener<CategoryData> {

//    因为CategoryExcelListener不能交给spring管理,需要自己new,不能注入其他对象
//    不能实现数据库操作
    public AutomobileCategoryService automobileCategoryService;

    public CategoryExcelListener() {
    }

    public CategoryExcelListener(AutomobileCategoryService automobileCategoryService) {
        this.automobileCategoryService = automobileCategoryService;
    }


    //    读取excel内容一行一行读取
    @Override
    public void invoke(CategoryData categoryData, AnalysisContext analysisContext) {

        if (categoryData == null) {
            throw new LException(500, "文件数据为空");
        }
//        一行一行读取,每次读取有两个值,第一个值一级分类,第二个值二级分类

//        判断一级分类是否重复
        AutomobileCategory automobileCategory = this.existOneCategory(automobileCategoryService, categoryData.getOneCategoryName());
        if (automobileCategory == null) {  //没有相同一级分类,进行添加
            automobileCategory = new AutomobileCategory();
            automobileCategory.setParentId(0);
            automobileCategory.setName(categoryData.getOneCategoryName()); //一级分类
            automobileCategoryService.save(automobileCategory);
        }

//        获取一级分类的id值
        Integer pid = automobileCategory.getId();

//        判断二级分类是否重复
        AutomobileCategory automobileCategoryTwo = this.existTwoCategory(automobileCategoryService, categoryData.getTwoCategoryName(), pid);
        if (automobileCategoryTwo == null) {  //没有相同的二级分类,进行添加
            automobileCategoryTwo = new AutomobileCategory();
            automobileCategoryTwo.setParentId(pid);
            automobileCategoryTwo.setName(categoryData.getTwoCategoryName()); //二级分类名称
            automobileCategoryService.save(automobileCategoryTwo);
        }

    }


    //    判断一级分类不能重复添加
    private AutomobileCategory existOneCategory(AutomobileCategoryService automobileCategoryService,
                                                String name) {
        QueryWrapper<AutomobileCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        wrapper.eq("parent_id", 0);
        AutomobileCategory one = automobileCategoryService.getOne(wrapper);
        return one;
    }

    //    判断二级分类不能重复添加
    private AutomobileCategory existTwoCategory(AutomobileCategoryService automobileCategoryService,
                                                String name, Integer pid) {
        QueryWrapper<AutomobileCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        wrapper.eq("parent_id", pid);
        AutomobileCategory tow = automobileCategoryService.getOne(wrapper);
        return tow;
    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }


}
